﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PagedList;
using PMKB_DAL;

namespace PMKB_Repository
{
    public interface ISearch
    {
        int? PageNumber { get; set; }
        int? PageSize{ get; set; }
        string OrderBy{ get; set; }
        bool? Asc { get; set; }
        string Search { get; set; }
        void DefaultValues();
    }
    public class SearchBase : ISearch
    {
        public int? PageNumber { get; set; }
        public int? PageSize { get; set; }
        public string OrderBy { get; set; }
        public bool? Asc { get; set; }
        public string Search{ get; set; }
        public void DefaultValues()
        {
            PageNumber = PageNumber ?? 1;
            PageSize = PageSize ?? 10;
            Asc = Asc ?? true;
        }
    }
    public class UserListViewModel
    {
        public ISearch Search;
        public  IPagedList< I_User> items; 
    }

    public class UserLoad
    {
        public string Connection;
        public UserListViewModel LoadUsers(ISearch s)
        {
        

            s.DefaultValues();
            if (string.IsNullOrEmpty(s.OrderBy))
            {
                s.OrderBy = User_List.FieldNames.UserName;
            }
            using (User_List ul = new User_List(Connection))
            {
               
                var iq=ul.LoadFromDB.LoadAllQ();
                IOrderedQueryable<User> ordered = User_FindDB.OrderByField(iq, s.OrderBy);
                if (!string.IsNullOrEmpty(s.Search))
                {
                    string search = s.Search.ToLower();
                    ul.LoadFromDB.AddToCustomPredicate(User_FindDB.fexp_UserNameContainsMultipleDef(search));
                    ordered=ul.LoadFromDB.LoadFindCustomPredicateOrderedQ(s.OrderBy);
                }
                int i = (ordered.Count());
                UserListViewModel ulvm = new UserListViewModel();
                ulvm.items = ordered.ToPagedList(s.PageNumber.Value, s.PageSize.Value);
                i = ulvm.items.TotalItemCount;
                ulvm.Search = s;
                return ulvm;
            }
        }

        public I_User CreateUser(I_User newUser)
        {

            User u = new User(newUser, WithPrimaryKey: false);
            using (User_List ul = new User_List(Connection))
            {
                ul.Add(u);
                ul.SaveNew();
            }
            return u;
        }

        public object LoadUser(long id)
        {
            using (User_List ul = new User_List(Connection))
            {
                ul.LoadFromDB.LoadFrom_UserID(id);
                return ul[0];
            }
        }

        public void SaveUser(User u)
        {
            using (User_List ul = new User_List(Connection))
            {
                ul.LoadFromDB.LoadFrom_UserID(u.UserID);
                var user=ul[0];
                user.CopyFromItemUser(u, WithPrimaryKey: false);
                ul.SaveExisting();
            }
        }

        public void Delete(long id, string userName)
        {
            using (User_List ul = new User_List(Connection))
            {
                ul.Delete_UserID(id);
            }
        }
    }



}
